#include <stdio.h>
#include <string.h>

char tu[25][25];
char flag[128];
int h, w, ans;
int movex[] = {-1, 0, 0, 1};
int movey[] = {0 ,-1, 1, 0};

void di(int x, int y, int length) {
    int tox, toy;
    if (ans < length) ans = length;
    for (int i=0; i<4; ++i) {
        tox = x + movex[i];
        toy = y + movey[i];
        if (tox>=0 && toy>=0 && tox<h && toy<w && flag[tu[tox][toy]]==0) {
            flag[tu[tox][toy]] = 1;
            di(tox, toy, length + 1);
            flag[tu[tox][toy]] = 0;
        }    
    }    
}    

int main()
{
    int i, j, k;
    while (scanf("%d%d", &h, &w)!=EOF) {
        for (i=0; i<h; ++i)
            scanf("%s", tu[i]);
        ans = 0;
        memset(flag, 0, sizeof(flag));
        flag[tu[0][0]] = 1;
        di(0, 0, 1);
        printf("%d\n", ans);
    }    
    
    return 0;
}    
